const Log = require('../models/Log');

class LogService {
  // 创建日志
  async createLog({ message, category }) {
    try {
      return await Log.create({
        message,
        category,
      });
    } catch (error) {
      throw new Error('创建日志失败');
    }
  }

  // 获取所有日志
  async getAllLogs() {
    try {
      return await Log.findAll();
    } catch (error) {
      throw new Error('获取日志列表失败');
    }
  }

  // 获取单个日志
  async getLogById(id) {
    try {
      return await Log.findByPk(id);
    } catch (error) {
      throw new Error('获取日志详情失败');
    }
  }

  // 更新日志
  async updateLog({ logId, message, category }) {
    try {
      const [updatedRows] = await Log.update(
        { message, category },
        { where: { id: logId } }
      );
      return updatedRows ? true : false;
    } catch (error) {
      throw new Error('更新日志失败');
    }
  }

  // 删除日志
  async deleteLog(logId) {
    try {
      const deletedRows = await Log.destroy({ where: { id: logId } });
      return deletedRows ? true : false;
    } catch (error) {
      throw new Error('删除日志失败');
    }
  }

  // 批量删除日志
  async deleteLogs({ logIds }) {
    try {
      const deletedRows = await Log.destroy({ where: { id: logIds } });
      return deletedRows ? true : false;
    } catch (error) {
      throw new Error('批量删除日志失败');
    }
  }
}

module.exports = new LogService();